import java.util.Collection;
import java.sql.*;

/**
 * This is the class that stores all of the patient's personal information
 */
public class PatientInfo {
	private Patient patient;
	private Address address;
	private long phoneNumber;
	private String email;
	private String gender;
	private int age;
	private String pharmacy;
	private String insuranceCarrier;
	private String allergies;
	private Collection<TreatmentRecord> medicalHistory;

	/**
	 * This is the constructor for the patient info class that fills in all of
	 * the patient's information
	 * 
	 * @param patient
	 *            the patient
	 * @param address2
	 *            the patient's address, in string form: {street, city, state,
	 *            zip}
	 * @param number
	 *            the patient's phone number
	 * @param email
	 *            the patient's email address
	 * @param gender
	 *            the patient's gender
	 * @param age
	 *            the patient's age
	 * @param pharmacy
	 *            the patient's pharmacy
	 * @param insurance
	 *            the patient's insurance
	 * @param allergies
	 *            the patient's allergies
	 * @param history
	 *            the collection of treatmentrecords
	 */
	public PatientInfo(Patient patient, String[] address2, long number,
			String email, String gender, int age, String pharmacy,
			String insurance, String allergies,
			Collection<TreatmentRecord> history) {
		this.patient = patient;
		this.address = new Address(address2[0], address2[1], address2[2],
				Integer.parseInt(address2[3]));
		this.phoneNumber = number;
		this.email = email;
		this.gender = gender;
		this.age = age;
		this.pharmacy = pharmacy;
		this.insuranceCarrier = insurance;
		this.allergies = allergies;
		this.medicalHistory = history;
	}

	// CRUD functionality
	// Create
	public static boolean createPatientInfo(Patient patient, Address address,
			long number, String email, String gender, int age, String pharmacy,
			String insurance, String allergies,
			Collection<TreatmentRecord> history) {
		boolean returnval = 1 == Database
				.query("INSERT INTO "
						+ "PatientInfo"
						+ " (username,phone_number,email,gender,age,pharmacy,insurance_carrier,allergies)"
						+ " VALUES (" + "'"
						+ patient.getUsername()
						+ "'"
						+ "'"
						+ number
						+ "'"
						+ "'"
						+ email
						+ "'"
						+ "'"
						+ gender
						+ "'"
						+ "'"
						+ age
						+ "'"
						+ "'"
						+ pharmacy
						+ "'"
						+ "'"
						+ insurance
						+ "'"
						+ "'" + allergies + "'" + ")");
		return returnval;
	}

	// Read
	public static PatientInfo readPatientInfo(String username) {
		long foundNumber = 0;
		String foundEmail = "";
		String foundGender = "";
		int foundAge = 0;
		String foundPharmacy = "";
		String foundInsurance = "";
		String foundAllergies = "";
		String sql = "SELECT * FROM PatientInfo WHERE username='" + username
				+ "'";
		ResultSet rs = Database.selectQuery(sql);
		// should only have 1 record per username
		try {
			while (rs.next()) {
				foundNumber = rs.getLong("phone_number");
				foundEmail = rs.getString("email");
				foundGender = rs.getString("gender");
				foundAge = rs.getInt("age");
				foundPharmacy = rs.getString("pharmacy");
				foundInsurance = rs.getString("insurance_carrier");
				foundAllergies = rs.getString("allergies");
			}
		} catch (Exception e) {
		}
		Patient patient = Patient.readPatient(username);
		Address address = Address.readAddress(username);
		String[] addressArray = new String[4];
		addressArray[0] = address.getStreet();
		addressArray[1] = address.getCity();
		addressArray[2] = address.getState();
		addressArray[3] = Integer.toString(address.getZipCode());
		PatientInfo foundInfo = new PatientInfo(patient, addressArray,
				foundNumber, foundEmail, foundGender, foundAge, foundPharmacy,
				foundInsurance, foundAllergies, null);
		return foundInfo;
	}

	// Update
	public static boolean updatePatientInfo(Patient patient, Address address,
			long number, String email, String gender, int age, String pharmacy,
			String insurance, String allergies,
			Collection<TreatmentRecord> history) {
		String sql = "UPDATE PatientInfo SET phone_number=" + number
				+ ", email='" + email + "', gender='" + gender + "', age="
				+ age + ", pharmacy='" + pharmacy + "', insurance_carrier='"
				+ insurance + "', allergies='" + allergies
				+ "' WHERE username='" + patient.getUsername() + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deletePatientInfo(String username) {
		String sql = "DELETE FROM PatientInfo WHERE username='" + username
				+ "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Returns the patient
	 * 
	 * @return the patient
	 */
	public Patient getPatient() {
		return patient;
	}

	/**
	 * This method sets a new patient
	 * 
	 * @param patient
	 *            a valid Patient object
	 */
	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	/**
	 * Returns the patient's address
	 * 
	 * @return the patient's address
	 */
	public Address getAddress() {
		return address;
	}

	/**
	 * Sets the patient's address
	 * 
	 * @param address
	 *            the patient's address
	 */
	public void setAddress(Address address) {
		this.address = address;
	}

	/**
	 * Returns the patient's phone number
	 * 
	 * @return the patient's phone number
	 */
	public long getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * Sets the patient's phone number
	 * 
	 * @param phoneNumber
	 *            the patient's phone number
	 */
	public void setPhoneNumber(long phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * Returns the patient's email
	 * 
	 * @return the patient's email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Sets the patient's email
	 * 
	 * @param email
	 *            the patient's email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Returns the patient's gender
	 * 
	 * @return the patient's gender
	 */
	public String getGender() {
		return gender;
	}

	/**
	 * Sets the patient's gender
	 * 
	 * @param gender
	 *            the patient's gender
	 */
	public void setGender(String gender) {
		this.gender = gender;
	}

	/**
	 * Returns the patient's age
	 * 
	 * @return the patient's age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * Sets the patient's age
	 * 
	 * @param age
	 *            the patient's age
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * Returns the patient's pharmacy
	 * 
	 * @return the patient's pharmacy
	 */
	public String getPharmacy() {
		return pharmacy;
	}

	/**
	 * Sets the patient's pharmacy
	 * 
	 * @param pharmacy
	 *            the name of the patient's pharmacy
	 */
	public void setPharmacy(String pharmacy) {
		this.pharmacy = pharmacy;
	}

	/**
	 * Returns the patient's insurance carrier
	 * 
	 * @return the patient's insurance carrier
	 */
	public String getInsuranceCarrier() {
		return insuranceCarrier;
	}

	/**
	 * Sets the patient's insurance carrier
	 * 
	 * @param carrier
	 *            the patient's insurance carrier
	 */
	public void setInsuranceCarrier(String carrier) {
		this.insuranceCarrier = carrier;
	}

	/**
	 * Returns the patient's allegies
	 * 
	 * @return the patient's allergies
	 */
	public String getAllergies() {
		return allergies;
	}

	/**
	 * Sets the patient's allergies
	 * 
	 * @param allergies
	 *            the patient's allergies
	 */
	public void setAllergies(String allergies) {
		this.allergies = allergies;
	}

	/**
	 * Returns the patient's medical history
	 * 
	 * @return the patient's medical history
	 */
	public Collection<TreatmentRecord> getMedicalHistory() {
		return medicalHistory;
	}

	/**
	 * Sets the patient's medical history
	 * 
	 * @param history
	 *            the patient's medical history
	 */
	public void setMedicalHistory(Collection<TreatmentRecord> history) {
		this.medicalHistory = history;
	}

}
